<template>
  <div class="login-container">
    <el-form :model="ruleForm2"
             :rules="rules2"
             status-icon
             ref="ruleForm2"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm login-page">
      <h3 class="title">系统登录</h3>
      <el-form-item prop="account">
        <el-input type="text"
                  v-model="ruleForm2.account"
                  auto-complete="off"
                  placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password"
                  v-model="ruleForm2.password"
                  auto-complete="off"
                  placeholder="密码"></el-input>
      </el-form-item>
      <el-checkbox v-model="checked"
                   class="rememberme">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button type="primary"
                   style="width:100%;"
                   @click="handleSubmit"
                   :loading="logining">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from '../api/user'
import bus from '../utils/busutils'
export default {
  data () {
    return {
      logining: false,
      ruleForm2: {
        account: '',
        password: ''
      },
      rules2: {
        account: [{ required: true, message: 'please enter your account', trigger: 'blur' }],
        password: [{ required: true, message: 'enter your password', trigger: 'blur' }]
      },
      checked: false
    }
  },
  methods: {
    // withCredentials: true //使得后台可以接收表单数据  跨域请求
    handleSubmit (event) {
      this.$refs.ruleForm2.validate((valid) => {
        if (valid) {
          let param = new FormData() // 创建form对象
          param.append('account', this.ruleForm2.account) // 通过append向form对象添加数据
          param.append('password', this.$md5(this.ruleForm2.password)) // 添加form表单中其他数据
          var params = {
            'account': this.ruleForm2.account, 'password': this.$md5(this.ruleForm2.password)
          }
          login('/user/login', params).then(data => {
            console.log(data)
            if (data.code === 200) {
              setTimeout(() => {
                // 将token和当前用户信息存入localStorage中
                localStorage.token = data.data[0].token
                localStorage.user = JSON.stringify(data.data[0].user)
                localStorage.setItem('username', data.data[0].user.account)
                this.$message('登录成功')
                bus.$emit('loginok', 'ok')
                this.$router.push({ path: '/home' })
              }, 2000)
            }
            if (data.code === 500) {
              this.$message.error(data.data)
              this.$router.push({ path: '/login' })
            }
          })
            .catch(data => {
              this.$message(data.list)
              this.$router.push({ path: '/user/login' })
            })
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100%;
}
.login-page {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
label.el-checkbox.rememberme {
  margin: 0px 0px 15px;
  text-align: left;
}
</style>
